package com.tenement.action;

import com.tenement.domain.*;
import com.tenement.service.GuideService;
import com.tenement.utils.GuideVo;
import com.tenement.utils.Tools;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @ClassName : guideHouseAction  //类名
 * @Description : 带看房源  //描述
 * @Author : 易嵘  //作者
 * @Date: 2020-07-10 16:10  //时间
 */
@Controller
@RequestMapping("guideAction")
public class GuideAction {

    @Resource
    GuideService guideService;

    /**
     * 查询带看房源
     * @param clientId
     * @return
     */
    @ResponseBody
    @RequestMapping("findHouseAll")
    public Map<String,Object> findHouseAll(String clientId){
        List<GuideVo> list=guideService.findHouseAll(clientId);
        return Tools.maps(0l,list);
    }

    /**
     * 新增带看房源
     * @param guideVo
     * @return
     */
    @ResponseBody
    @RequestMapping("addGuideHouse")
    public int addGuideHouse(GuideVo guideVo, HttpServletRequest request, String clientId, @RequestParam(value = "imgs[]",required = false) String[] imgs){
        TenementUser user= (TenementUser) request.getSession().getAttribute("user");
        if(StringUtils.isEmpty(guideVo.getGuideId())){
            return guideService.addGuideHouse(guideVo,user,clientId,imgs,request.getRemoteAddr());
        }else{
            return guideService.updateGuideHouse(guideVo,user,clientId,imgs,request.getRemoteAddr());
        }

    }

    /**
     * id查询带看
     * @param guideId 带看id
     * @return
     */
    @ResponseBody
    @RequestMapping("findById")
    public GuideVo findById(String guideId){
        return guideService.findById(guideId);
    }

    /**
     * 删除带看
     * @param guideHouseId
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteGuideHouse")
    public int deleteGuideHouse(String guideHouseId,HttpServletRequest request){
        TenementUser user= (TenementUser) request.getSession().getAttribute("user");
        return guideService.deleteGuideHouse(guideHouseId,request.getRemoteAddr(),user.getUserName());
    }

    /**
     * 查询带看信息
     * @param clientId
     * @return
     */
    @ResponseBody
    @RequestMapping("findClientId")
    public Map<String,Object> findClientId(String clientId){
        return guideService.findClientId(clientId);
    }

    /**
     * 新增带看
     * @param guide
     * @return
     */
    @ResponseBody
    @RequestMapping("addGuide")
    public int addGuide(TenementGuide guide){
        return guideService.addGuide(guide);
    }

    /**
     * 新增带看人员
     * @param guideUser
     * @return
     */
    @ResponseBody
    @RequestMapping("addGuideUser")
    public int addGuideUser(TenementGuideUser guideUser){
        return guideService.addGuideUser(guideUser);
    }

    /**
     * 删除带看人员
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteGuideUser")
    public int deleteGuideUser(String id){
        return guideService.deleteGuideUser(id);
    }

    /**
     * 查询带看人员
     * @param guideId
     * @return
     */
    @ResponseBody
    @RequestMapping("findGuideUser")
    public List<TenementGuideUser> findGuideUser(String guideId){
        return guideService.findGuideUser(guideId);
    }

    /**
     * 新增图片
     * @param guideImg
     * @return
     */
    @ResponseBody
    @RequestMapping("addImg")
    public int addImg(TenementGuideImg guideImg){
        return guideService.addImg(guideImg);
    }

    /**
     * 删除图片
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("deleteImg")
    public int deleteImg(String id){
        return guideService.deleteImg(id);
    }

    /**
     * 查询图片
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping("findGuideImgAll")
    public List<TenementGuideImg> findGuideImgAll(String id){
        return guideService.findGuideImgAll(id);
    }

    /**
     * 日期格式化
     *
     * @param binder
     */
    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true)); // true:允许输入空值，false:不能为空值

    }
}
